//
//  RoomTimeController.m
//  QRMaster
//
//  Created by myApple on 15/8/13.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "RoomTimeController.h"
#import "SetDateTimeCell.h"
#import "setTimeModel.h"
#import "PromptInfoView.h"
#import "RequestNetWork.h"
#import "Reachability.h"

@interface RoomTimeController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * timeTableView;
    NSMutableArray * timeDataArray;
    NSArray * weakData;
    
    //多语言
    UILabel * titleLabel;
}

@end

@implementation RoomTimeController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareTableView];
    [self getData];
    timeDataArray = [[NSMutableArray alloc]init];
    weakData = @[NSLocalizedString(@"Sunday", nil),NSLocalizedString(@"Monday", nil),NSLocalizedString(@"Tuesday", nil),NSLocalizedString(@"Wednesday", nil),NSLocalizedString(@"Thursday", nil),NSLocalizedString(@"Friday", nil),NSLocalizedString(@"Saturday", nil)];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.title = NSLocalizedString(@"lookTime", nil);
}

-(void)getData
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:_communityId forKey:@"communityId"];
    [postDict setValue:_roomId forKey:@"roomId"];
    [rnk initWithPath:POST(GETMYSELFROOMTINE) params:postDict block:^(id downLoadData, id error) {
        NSDictionary * data = [downLoadData objectForKey:@"data"];
        if (data != nil)
        {
            if ([[data objectForKey:@"flag"]intValue] == 0) {
                NSArray * listArray = [data objectForKey:@"list"];
                for (NSDictionary * dict in listArray)
                {
                    setTimeModel * model = [[setTimeModel alloc]init];
                    model.beginTime = [dict objectForKey:@"startTime"];
                    model.endTime = [dict objectForKey:@"endTime"];
                    model.weekInfo = [dict objectForKey:@"cycleString"];
                    model.timeId = [dict objectForKey:@"id"];
                    [timeDataArray addObject:model];
                }
                [timeTableView reloadData];
                if (timeDataArray.count == 0)
                {
                    [PromptInfoView showMessage:NSLocalizedString(@"noTimeOpenRoom", nil)];
                }
            }
            else
            {
                [PromptInfoView showMessage:NSLocalizedString(@"allTimeOpen", nil)];
            }
        }
        else if (error != nil)
        {
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
        }
        [self stopWaiting];
    }];
}

-(void)prepareTableView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight - 64;
    
    timeTableView = [[UITableView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    timeTableView.dataSource = self;
    timeTableView.delegate = self;
    timeTableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:timeTableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return timeDataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * string = @"cell";
    SetDateTimeCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[SetDateTimeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    if (timeDataArray.count > indexPath.row) {
        setTimeModel * model = timeDataArray[indexPath.row];
        cell.beginTime = model.beginTime;
        cell.endTime = model.endTime;
        cell.weekInfo = model.weekInfo;
        cell.showSwitch = NO;
    }
     return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
